package cn.lyd.basic.controller;

import cn.lyd.basic.exception.BusinessException;
import cn.lyd.basic.service.IVerifyCodeService;
import cn.lyd.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 验证码接口类
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService VerifyCodeService;

    /**
     * 接口：图形验证码
     * @param key
     * @return
     */
    @GetMapping("image/{key}")
    public  String getImageCode(@PathVariable("key") String key){
        return  VerifyCodeService.getImageCode(key);

    }


    /**
     * 手机号注册时发送手机短信验证码
     * @param map
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult getSmsCode(@RequestBody Map<String,String> map){
        try {
            VerifyCodeService.getSmsCode(map);
            return  new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return  new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,"系统繁忙,请稍后重试！");
        }
    }



    /**
     * 微信绑定时发送手机短信验证码
     * @param map
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody Map<String,String> map){
        try {
            VerifyCodeService.binderSmsCode(map);
            return  new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return  new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,"系统繁忙,请稍后重试！");
        }
    }


}
